package com.xsmart.oa.dao;

import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/21.
 */
@Component
public interface QueryAdminManageDAO {

    @Select("<script>select user_id,user_name,email,password,DATE_FORMAT(add_time,'%Y-%m-%d %T')add_time,DATE_FORMAT(last_login,'%Y-%m-%d %T')last_login,last_ip,action_list,nav_list,(case when role_id='0' then '平台管理员' when role_id='1' then '供应商管理员' else '运营人员' end)role_id from INFS_ADMIN_USER WHERE <if test=\"userName==null or userName==''\">1=1</if><if test=\"userName!=null and userName!=''\">user_name like concat('%',#{userName},'%') </if><if test=\"roleId!=null\">and role_id=#{roleId}</if></script>")
    List<Map<String,Object>> queryAdmin(Pagination page,@Param("userName")String userName,@Param("roleId")Integer roleId);

    /**
     * 查询对应权限编码的权限说明
     * @param actionCode
     * @return
     */
    @Select("<script>select action_desc from cfgs_action where action_code in" +
            "<foreach collection=\"actionCode\" item=\"item\" index=\"index\" \n" +
                "open=\"(\" separator=\",\" close=\")\">#{item}" +
            "</foreach>" +
            "</script>")
    List<String>queryActionDesc(@Param("actionCode")String[]actionCode);

    @Delete("delete from INFS_ADMIN_USER where user_id=#{userId}")
    int deleteAdminById(@Param("userId") Integer userId);

    @Update("update INFS_ADMIN_USER set " +
            "user_name=#{adminMap.userName}," +
            "email=#{adminMap.email}," +
//            "password=#{adminMap.password}," +
            "add_time=#{adminMap.addTime}," +
//            "last_login=#{adminMap.lastLogin}," +
//            "last_ip=#{adminMap.lastIp}," +
            "action_list=#{adminMap.actionList}," +
//            "nav_list=#{adminMap.navList}," +
            "role_id=#{adminMap.roleId} " +
            "where user_id=#{adminMap.userId}")
    int updateAdminById(@Param("adminMap") Map<String, Object> adminMap);

    @Insert("insert into INFS_ADMIN_USER " +
            "(user_id," +
            "user_name," +
            "email," +
            "password," +
            "add_time," +
            "last_login," +
            "last_ip," +
            "action_list," +
            "nav_list," +
            "role_id) " +
            "values" +
            "(null," +
            "#{adminMap.userName}," +
            "#{adminMap.email}," +
            "#{adminMap.password}," +
            "now()," +
            "now()," +
            "#{adminMap.lastIp}," +
            "#{adminMap.actionList}," +
            "#{adminMap.navList}," +
            "#{adminMap.roleId})")
    int saveAdmin(@Param("adminMap")Map<String, Object> adminMap);


}
